package com.example.himalayaproject.subscription.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.himalayaproject.R;
import com.example.himalayaproject.subscription.bean.detailsentity.DateEntity;
import com.example.himalayaproject.subscription.view.CircleTransformation;
import com.squareup.picasso.Picasso;

import java.text.DecimalFormat;

/**
 * Created by Administrator on 2016/12/9.
 */
public class FragmentSepciaDetailsAdapter extends RecyclerView.Adapter {
    private Context context;
    private DateEntity date;
    private final int item1_TYPWE = 1;
    private final int item2_TYPWE = 2;
    private final int item3_TYPWE = 3;
    private int item3_count = -1;
    private boolean state = false;

    public FragmentSepciaDetailsAdapter(Context context) {
        this.context = context;
        date = new DateEntity();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case item1_TYPWE:
                return new Item1ViewHolder(LayoutInflater.from(context).inflate(R.layout.fragment_special_item1, parent, false));
            case item2_TYPWE:
                return new Item2ViewHolder(LayoutInflater.from(context).inflate(R.layout.fragment_special_item2, parent, false));
            case item3_TYPWE:
                return new Item3ViewHolder(LayoutInflater.from(context).inflate(R.layout.fragment_special_item3_list, parent, false));
        }
        return null;
    }

    public void addEntity(DateEntity dateEntity) {
        date = dateEntity;
        notifyItemRangeInserted(0,1);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof Item1ViewHolder) {
            ((Item1ViewHolder) holder).textView.setText(date.getDetail().getIntro());
        } else if (holder instanceof Item2ViewHolder) {
            ((Item2ViewHolder) holder).nickName.setText(date.getUser().getNickname());
            DecimalFormat decimalFormat = new DecimalFormat(".0");
            double number = Integer.parseInt(date.getRecs().getList().get(position).getPlaysCounts()) / 10000;
            if (number > 9999) {
                number = number / 10000;
                ((Item2ViewHolder) holder).nickFollow.setText("已被"+decimalFormat.format(number) + "亿人关注");
            } else {
                ((Item2ViewHolder) holder).nickFollow.setText("已被"+decimalFormat.format(number) + "万人关注");
            }
            ((Item2ViewHolder) holder).theme.setText(date.getUser().getPersonalSignature());
            Picasso.with(context).load(date.getUser().getSmallLogo()).transform(new CircleTransformation()).into(((Item2ViewHolder) holder).imageView);
        } else if (holder instanceof Item3ViewHolder) {
            FragmentSepcialDetailsAdapterItem3Adapter adapter = new FragmentSepcialDetailsAdapterItem3Adapter(context, date);
            ((Item3ViewHolder) holder).recyclerView.setAdapter(adapter);
        }
    }


    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return item1_TYPWE;
            case 1:
                return item2_TYPWE;
            case 2:
                return item3_TYPWE;
        }
        return  -2;
    }

    @Override
    public int getItemCount() {
        return 3;
    }


    public static class Item1ViewHolder extends RecyclerView.ViewHolder {
        private TextView textView;
        private Button button;

        public Item1ViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.fragment_special_item1_textview);
            button = (Button) itemView.findViewById(R.id.fragment_special_item1_button);
        }
    }

    public static class Item2ViewHolder extends RecyclerView.ViewHolder {
        private TextView nickName, nickFollow, theme;
        private ImageView imageView;

        public Item2ViewHolder(View itemView) {
            super(itemView);
            nickName = (TextView) itemView.findViewById(R.id.fragment_special_item2_nickName);
            nickFollow = (TextView) itemView.findViewById(R.id.fragment_special_item2_nickFollow);
            theme = (TextView) itemView.findViewById(R.id.fragment_special_item2_theme);
            imageView = (ImageView) itemView.findViewById(R.id.fragment_special_item2_nickImage);
        }
    }

    public static class Item3ViewHolder extends RecyclerView.ViewHolder {
        private RecyclerView recyclerView;

        public Item3ViewHolder(View itemView) {
            super(itemView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.fragment_spcial_item3_recycler);
        }
    }
}
